package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.DesignerDaolmpl;
import com.ysd.dao.impl.ProductcategoryDaolmpl;
import com.ysd.entity.Designer;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminDesignerServlet extends BaseServlet {
       
	//��ȡ���ʦ����
	//·����localhost:8080/dayixiacedexiangmu/adminDesigner?reqName=getDesigner
	public void getDesigner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DesignerDaolmpl dDao = new DesignerDaolmpl();
		List<Designer> dist =dDao.getDesigner();//���в�Ʒ����
		
		request.setCharacterEncoding("UTF-8");//������Ӧ�ı����ʽ
		PrintWriter out = response.getWriter();//out����
		
		Gson gson = new Gson();//����Gson����
		
		String res = gson.toJson(dist);//��pcListת��Ϊjson��ʽ�ַ���
		out.print(res);//��Ӧ
		
		out.flush();
		out.close();
	}
	//��ȡ���ʦ����
		//·����localhost:8080/dayixiacedexiangmu/adminDesigner?reqName=delDesigner
	public void delDesigner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//����ǰ̨�����Ĳ���
		int dgId = Integer.parseInt(request.getParameter("dgId"));
		DesignerDaolmpl pcDao = new DesignerDaolmpl();
		request.setCharacterEncoding("UTF-8");//������Ӧ�ı����ʽ
		PrintWriter out = response.getWriter();//out����
		int n  = pcDao.delDesignerId(dgId);//����ʵ�����ɾ��������������Ӱ��
		if (n>0) {//ɾ���ɹ�
			out.print(Result.toClient(true, "ɾ���ɹ�",""));
		}else {//ɾ������
			out.print(Result.toClient(false, "ɾ��ʧ��",""));
		}
		
		
		out.flush();
		out.close();
	}
	public void addDesigner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		DesignerDaolmpl pcDao = new DesignerDaolmpl();
		String designerposition =request.getParameter("designerposition");
		String designerhead =request.getParameter("designerhead");
		String designername =request.getParameter("designername"); 
		if(designername !=null && !designername.equals("")) {
			List<Designer> deList=pcDao.getdesignername(designername);
			if (deList.size()  == 0) {
				Designer de = new Designer(0, designername, designerposition, designerhead);
				
				
				int n  = pcDao.addDesignerId(de);//����ʵ�����ɾ��������������Ӱ��
				if (n>0) {//ɾ���ɹ�
					out.print(Result.toClient(true, "添加成功",""));
				}else {//ɾ������
					out.print(Result.toClient(false, "添加失败",""));
				}
				
			} else {
				out.print(Result.toClient(false, "设计师名称已存在",""));
			}
		}
		
		
		
		
		out.flush();
		out.close();
	}
	
public void geteditDesigner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		DesignerDaolmpl pcDao = new DesignerDaolmpl();
		int cateId = Integer.parseInt(request.getParameter("cId"));            
		Designer dc=pcDao.getdesignerid(cateId);
		Gson gson = new Gson();
		String res =gson.toJson(dc);
		out.print(res);
		
		out.flush();
		out.close();
	}
	 
public void getupdateDesigner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	request.setCharacterEncoding("UTF-8");
	PrintWriter out = response.getWriter();
	DesignerDaolmpl pcDao = new DesignerDaolmpl();
	int designerid =Integer.parseInt(request.getParameter("designerid"));
	String designerposition =request.getParameter("designerposition");
	String designerhead =request.getParameter("designerhead");
	String designername =request.getParameter("designername");
	if(designername !=null && !designername.equals("")) {
		List<Designer> deList=pcDao.getdesignername(designername);
		if (deList.size()  == 0) {
			Designer dc=new Designer(designerid, designername, designerposition, designerhead);
			int n= pcDao.updateDesigner(dc);
			if (n>0) {//ɾ���ɹ�
				out.print(Result.toClient(true, "修改成功",""));
			}else {//ɾ������
				out.print(Result.toClient(false, "修改失败",""));
			}
			
		} else {
			out.print(Result.toClient(false, "设计师名称已存在",""));
		}
	}
	
	out.flush();
	out.close();
}

public void uploadImg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	response.setCharacterEncoding("UTF-8");
	// 设置编码格式
	PrintWriter out = response.getWriter(); 
	// 1.创建SmartUpload
	SmartUpload smart = new SmartUpload(); 
	// 2.设置字符集
	smart.setCharset("UTF-8"); 
	// 3.初始化
	smart.initialize(getServletConfig(), request, response);
	
	try { // 4.上传文件 smart.upload(); // 5.保存文件到指定位置(save只提供路径，使用原文件名上传文件)
			// 
		smart.upload();
		// 5.保存文件到指定位置(saveAs可以用指定名称上传文件) //
			// 
		String filename= System.currentTimeMillis()+".jpg";
		smart.getFiles().getFile(0).saveAs("/uploadImg/ssj/"+filename);
			// // 获取文件名
		filename = "/uploadImg/ssj/"+filename;
		out.print(Result.toClient(true, "上传成功",filename));
	} catch (SmartUploadException e) {
		out.print(Result.toClient(false, "上传失败", "图片过大"));
		e.printStackTrace();
	}
	out.flush();
	out.close();
}
}
